package st.business.tepth.business.login;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.SpannedString;
import android.text.style.AbsoluteSizeSpan;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import butterknife.BindView;
import butterknife.OnClick;
import st.business.tepth.R;
import st.business.tepth.R2;
import st.business.tepth.manager.UserBean;
import st.business.tepth.manager.UserManager;
import st.shadow.library.core.base.mvp.MvpFragment;
import st.shadow.library.core.storage.ShadowPreference;
import st.shadow.library.ui.dialog.DialogLoader;

/**
 * Author: shadow
 * Email:  1025431817@qq.com
 * Time:   2019-01-14 19:23
 */

public class LoginDelegate extends MvpFragment<LoginPresenter> implements
        LoginContract.View {
    @BindView(R2.id.et_account)
    EditText mEtAccount;
    @BindView(R2.id.et_password)
    EditText mEtPassword;
    private DialogLoader mDialogLoader;

    public static LoginDelegate newInstance() {
        return new LoginDelegate();
    }

    @Override
    public LoginPresenter getPresenter() {
        return new LoginPresenter(this);
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_login;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        Log.e("LoginDelegate", "onBindView");
        SpannableString spAccount = new SpannableString("请输入用户名");//定义hint的值
        AbsoluteSizeSpan spSizeAccountSpan = new AbsoluteSizeSpan(10, true);//设置字体大小 true表示单位是sp
        spAccount.setSpan(spSizeAccountSpan, 0, spAccount.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mEtAccount.setHint(new SpannedString(spAccount));

        SpannableString spPassword = new SpannableString("请输入密码");//定义hint的值
        AbsoluteSizeSpan spSizePassword = new AbsoluteSizeSpan(10, true);//设置字体大小 true表示单位是sp
        spPassword.setSpan(spSizePassword, 0, spPassword.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mEtPassword.setHint(new SpannedString(spPassword));

        mEtAccount.setText(UserManager.getInstance().getCurrentUserCode());
        mDialogLoader = new DialogLoader(_mActivity);
    }

    @OnClick(R2.id.btn_login)
    public void onLogin() {
        mDialogLoader.showProgressDialog(false, "正在登录", "请稍候...");
        mPresenter.login(mEtAccount.getText().toString(),
                mEtPassword.getText().toString(), 1);
    }


    @Override
    public void succeed(UserBean userBean) {
        mDialogLoader.clearDialog();
//        if (getParentFragment() != null) {
//            UserManager.getInstance().saveUser(userBean);
//            ((RootDelegate) getParentFragment()).onLogin();
//        }
        //第一次使用需要登录进行认证
        ShadowPreference.setAppFlag("verified", true);
        pop();
    }

    @Override
    public void failed(int code, String msg) {
        mDialogLoader.stopProgressWithFailed("登录失败", msg);
    }
}